package com.woniuxy.movie.order.controller;


import com.woniuxy.movie.commons.core.model.Result;
import com.woniuxy.movie.commons.web.annotation.MovieLog;
import com.woniuxy.movie.commons.web.model.PageInfo;
import com.woniuxy.movie.order.dto.OrderInfoDto;
import com.woniuxy.movie.order.form.CreateOrderForm;
import com.woniuxy.movie.order.form.orderinfo.PagePayForm;
import com.woniuxy.movie.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 * 电影票订单详情表 前端控制器
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@RestController
@RequestMapping("/orderInfo")
@Api(tags = "订单详情相关接口")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 下单
     * @param form
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("下单操作")
    public Result<OrderInfoDto> createOrder(@RequestBody CreateOrderForm form){
        OrderInfoDto orderInfoDto = orderInfoService.createOrder(form);
        if (orderInfoDto==null) {
            Result.getResult(OrderInfoDto.class).setMsg("下单失败").setCode(500);
        }
        return Result.getResult(OrderInfoDto.class).setMsg("下单成功").setData(orderInfoDto);
    }

    /**
     * 分页查询订单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPageOrders")
    @ApiOperation("查询所有订单操作")
    public Result<PageInfo> selectPageOrders(Integer pageNum, Integer pageSize){
        System.out.println(pageNum);
       PageInfo pageInfo = orderInfoService.selectPageOrders(pageNum,pageSize);
       return Result.getResult(PageInfo.class).setMsg("分页查询所有订单成功").setData(pageInfo);
    }

    /**
     * 查询单个订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/findSingleOrder")
    @ApiOperation("查询订单详情操作")
    @MovieLog("通过订单ID查询订单详情")
    public Result<OrderInfoDto> findSingleOrder(String orderId){
        OrderInfoDto orderInfoDto = orderInfoService.findSingleOrder(orderId);
        return Result.getResult(OrderInfoDto.class).setMsg("查询订单详情成功").setData(orderInfoDto);
    }

    @PostMapping("/pagePay")
    @ApiOperation("跳转支付宝页面")
    public Result pagePay(@RequestBody PagePayForm form) throws Exception {
        String page = orderInfoService.pagePay(form);
        return Result.getResult(String.class).setMsg("页面响应成功").setData(page);
    }

    @PostMapping("/notify")
    @ApiOperation("支付宝回调函数")
    public String notifyHandlerOrder(HttpServletRequest request) throws Exception {
        //异步回调主要代码
        //获取支付宝POST过来的反馈信息
        //在控制层从request取出支付宝传过来的参数
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        orderInfoService.handlerOrder(params);
        return "SUCCESS";
    }


}

